import { get, post, put, del } from '@/utils/request';

/**
 * 课程管理API服务
 */

// ==================== 课程管理 ====================

/**
 * 获取课程列表
 */
export async function getCourseList(params: COURSE.CourseListParams): Promise<COURSE.CourseListResponse> {
  return get('/admin/courses', params);
}

/**
 * 获取课程详情
 */
export async function getCourseDetail(id: number): Promise<COURSE.CourseDetailResponse> {
  return get(`/admin/courses/${id}`);
}

/**
 * 创建课程
 */
export async function createCourse(data: COURSE.CreateCourseParams): Promise<COURSE.ApiResponse<COURSE.CourseModel>> {
  return post('/admin/courses', data);
}

/**
 * 更新课程
 */
export async function updateCourse(id: number, data: COURSE.UpdateCourseParams): Promise<COURSE.ApiResponse<COURSE.CourseModel>> {
  return put(`/admin/courses/${id}`, data);
}

/**
 * 删除课程
 */
export async function deleteCourse(id: number): Promise<COURSE.ApiResponse<null>> {
  return del(`/admin/courses/${id}`);
}

/**
 * 批量删除课程
 */
export async function batchDeleteCourses(ids: number[]): Promise<COURSE.ApiResponse<null>> {
  return post('/admin/courses/batch-delete', { course_ids: ids });
}

/**
 * 更新课程状态
 */
export async function updateCourseStatus(id: number, status: COURSE.CourseStatus): Promise<COURSE.ApiResponse<null>> {
  return put(`/admin/courses/${id}/status`, { status });
}

/**
 * 批量更新课程状态
 */
export async function batchUpdateCourseStatus(ids: number[], status: COURSE.CourseStatus): Promise<COURSE.ApiResponse<null>> {
  return put('/admin/courses/batch-status', { course_ids: ids, status });
}

/**
 * 复制课程
 */
export async function copyCourse(id: number, title?: string): Promise<COURSE.ApiResponse<COURSE.CourseModel>> {
  return post(`/admin/courses/${id}/copy`, { title });
}

/**
 * 获取课程统计数据
 */
export async function getCourseStatistics(): Promise<COURSE.ApiResponse<COURSE.CourseStatistics>> {
  return get('/admin/statistics/courses');
}

// ==================== 课程分类管理 ====================

/**
 * 获取课程分类列表
 */
export async function getCategoryList(params: COURSE.CategoryListParams): Promise<COURSE.CategoryListResponse> {
  return get('/admin/course-categories', params);
}

/**
 * 获取所有课程分类（用于下拉选择）
 */
export async function getAllCategories(): Promise<COURSE.ApiResponse<COURSE.CategoryModel[]>> {
  return get('/admin/course-categories/all');
}

/**
 * 获取课程分类详情
 */
export async function getCategoryDetail(id: number): Promise<COURSE.CategoryDetailResponse> {
  return get(`/admin/course-categories/${id}`);
}

/**
 * 创建课程分类
 */
export async function createCategory(data: COURSE.CreateCategoryParams): Promise<COURSE.ApiResponse<COURSE.CategoryModel>> {
  return post('/admin/course-categories', data);
}

/**
 * 更新课程分类
 */
export async function updateCategory(id: number, data: COURSE.UpdateCategoryParams): Promise<COURSE.ApiResponse<COURSE.CategoryModel>> {
  return put(`/admin/course-categories/${id}`, data);
}

/**
 * 删除课程分类
 */
export async function deleteCategory(id: number): Promise<COURSE.ApiResponse<null>> {
  return del(`/admin/course-categories/${id}`);
}

/**
 * 更新分类状态
 */
export async function updateCategoryStatus(id: number, status: 'active' | 'inactive'): Promise<COURSE.ApiResponse<null>> {
  return put(`/admin/course-categories/${id}/status`, { status });
}

/**
 * 更新分类排序
 */
export async function updateCategorySort(data: Array<{ id: number; sort_order: number }>): Promise<COURSE.ApiResponse<null>> {
  return put('/admin/course-categories/sort', { categories: data });
}

// ==================== 课程进度管理 ====================

/**
 * 获取课程进度列表
 */
export async function getCourseProgressList(courseId: number, params?: { page?: number; limit?: number }): Promise<COURSE.ApiResponse<any>> {
  return get(`/admin/courses/${courseId}/progress`, params);
}

/**
 * 重置用户课程进度
 */
export async function resetCourseProgress(courseId: number, userId: number): Promise<COURSE.ApiResponse<null>> {
  return post(`/admin/courses/${courseId}/progress/${userId}/reset`);
}

/**
 * 批量重置课程进度
 */
export async function batchResetCourseProgress(courseId: number, userIds: number[]): Promise<COURSE.ApiResponse<null>> {
  return post(`/admin/courses/${courseId}/progress/batch-reset`, { user_ids: userIds });
}

// ==================== 课程详细信息 ====================

/**
 * 获取课程课时列表
 */
export async function getCourseLessons(courseId: number): Promise<COURSE.ApiResponse<COURSE.LessonModel[]>> {
  return get(`/admin/courses/${courseId}/lessons`);
}

/**
 * 获取课程学员列表
 */
export async function getCourseStudents(courseId: number, params?: { page?: number; limit?: number }): Promise<COURSE.ApiResponse<any>> {
  return get(`/admin/courses/${courseId}/students`, params);
}

// ==================== 课程评价管理 ====================

/**
 * 获取课程评价列表
 */
export async function getCourseReviews(courseId: number, params?: { page?: number; limit?: number }): Promise<COURSE.ApiResponse<any>> {
  return get(`/admin/courses/${courseId}/reviews`, params);
}

/**
 * 删除课程评价
 */
export async function deleteCourseReview(courseId: number, reviewId: number): Promise<COURSE.ApiResponse<null>> {
  return del(`/admin/courses/${courseId}/reviews/${reviewId}`);
}

/**
 * 批量删除课程评价
 */
export async function batchDeleteCourseReviews(courseId: number, reviewIds: number[]): Promise<COURSE.ApiResponse<null>> {
  return post(`/admin/courses/${courseId}/reviews/batch-delete`, { review_ids: reviewIds });
}

// ==================== 导入导出 ====================

/**
 * 导出课程数据
 */
export async function exportCourses(params: COURSE.CourseListParams & { format: 'excel' | 'csv' }): Promise<Blob> {
  // 注意：导出功能需要特殊处理，暂时保留原始实现
  return get('/admin/courses/export', params) as Promise<Blob>;
}

/**
 * 导入课程数据
 */
export async function importCourses(file: File): Promise<COURSE.ApiResponse<{ success_count: number; error_count: number; errors: any[] }>> {
  const formData = new FormData();
  formData.append('file', file);

  return post('/admin/courses/import', formData);
}

/**
 * 下载课程导入模板
 */
export async function downloadCourseTemplate(): Promise<Blob> {
  // 注意：下载功能需要特殊处理，暂时保留原始实现
  return get('/admin/courses/import-template') as Promise<Blob>;
}

// ==================== 章节管理 ====================

/**
 * 获取课程章节列表
 */
export async function getCourseChapters(courseId: number): Promise<COURSE.ApiResponse<COURSE.ChapterModel[]>> {
  return get(`/admin/courses/${courseId}/chapters`);
}

/**
 * 创建课程章节
 */
export async function createCourseChapter(courseId: number, data: COURSE.CreateChapterParams): Promise<COURSE.ApiResponse<COURSE.ChapterModel>> {
  return post(`/admin/courses/${courseId}/chapters`, data);
}

/**
 * 更新章节
 */
export async function updateChapter(chapterId: number, data: COURSE.UpdateChapterParams): Promise<COURSE.ApiResponse<COURSE.ChapterModel>> {
  return put(`/admin/chapters/${chapterId}`, data);
}

/**
 * 删除章节
 */
export async function deleteChapter(chapterId: number): Promise<COURSE.ApiResponse<null>> {
  return del(`/admin/chapters/${chapterId}`);
}

// ==================== 课时管理 ====================

/**
 * 创建课时
 */
export async function createLesson(courseId: number, data: COURSE.CreateLessonParams): Promise<COURSE.ApiResponse<COURSE.LessonModel>> {
  return post(`/admin/courses/${courseId}/lessons`, data);
}

/**
 * 获取课时详情
 */
export async function getLessonDetail(lessonId: number): Promise<COURSE.ApiResponse<COURSE.LessonModel>> {
  return get(`/admin/lessons/${lessonId}`);
}

/**
 * 更新课时
 */
export async function updateLesson(lessonId: number, data: COURSE.UpdateLessonParams): Promise<COURSE.ApiResponse<COURSE.LessonModel>> {
  return put(`/admin/lessons/${lessonId}`, data);
}

/**
 * 删除课时
 */
export async function deleteLesson(lessonId: number): Promise<COURSE.ApiResponse<null>> {
  return del(`/admin/lessons/${lessonId}`);
}

/**
 * 课时批量操作
 */
export async function batchOperateLessons(data: COURSE.LessonBatchParams): Promise<COURSE.ApiResponse<null>> {
  return post('/admin/lessons/batch', data);
}

/**
 * 调整课时顺序
 */
export async function reorderLessons(data: COURSE.LessonReorderParams): Promise<COURSE.ApiResponse<null>> {
  return put('/admin/lessons/reorder', data);
}

/**
 * 获取课程课时统计
 */
export async function getCourseLessonStats(courseId: number): Promise<COURSE.ApiResponse<COURSE.CourseLessonStatistics>> {
  return get(`/admin/courses/${courseId}/lessons/stats`);
}

/**
 * 获取课时学习统计
 */
export async function getLessonStats(lessonId: number): Promise<COURSE.ApiResponse<COURSE.LessonStatistics>> {
  return get(`/admin/lessons/${lessonId}/stats`);
}

// ==================== 其他功能 ====================

/**
 * 获取课程难度选项
 */
export async function getCourseDifficultyOptions(): Promise<COURSE.ApiResponse<Array<{ label: string; value: string }>>> {
  return get('/admin/courses/difficulty-options');
}

/**
 * 获取课程状态选项
 */
export async function getCourseStatusOptions(): Promise<COURSE.ApiResponse<Array<{ label: string; value: string }>>> {
  return get('/admin/courses/status-options');
}

/**
 * 检查课程标题是否可用
 */
export async function checkCourseTitleAvailable(title: string, excludeId?: number): Promise<COURSE.ApiResponse<{ available: boolean }>> {
  return get('/admin/courses/check-title', { title, exclude_id: excludeId });
}

/**
 * 获取课程标签建议
 */
export async function getCourseTagSuggestions(keyword?: string): Promise<COURSE.ApiResponse<string[]>> {
  return get('/admin/courses/tag-suggestions', { keyword });
}
